package com.ruoyi.web.controller.system;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.HatchetAcq;
import com.ruoyi.system.domain.HatchetAgent;
import com.ruoyi.system.domain.HatchetIssBank;
import com.ruoyi.system.domain.HatchetMerchant;
import com.ruoyi.system.domain.HatchetOrderPayment;
import com.ruoyi.system.service.IHatchetAcqService;
import com.ruoyi.system.service.IHatchetAgentService;
import com.ruoyi.system.service.IHatchetIssBankService;
import com.ruoyi.system.service.IHatchetMerchantService;
import com.ruoyi.system.service.IHatchetOrderPaymentService;

/**
 * 订单 信息操作处理(代理商操作)
 * 
 * @author xj
 * @date 2019-02-20
 */
@Controller
@RequestMapping("/system/hatchetOrderPaymentForDLS")//菜单请求路径
public class HatchetOrderPaymentForDLSController extends BaseController
{
    private String prefix = "system/hatchetOrderPayment";
	
	@Autowired
	private IHatchetOrderPaymentService hatchetOrderPaymentService;
	@Autowired
	private IHatchetMerchantService hatchetMerchantService;
	@Autowired
	private IHatchetAgentService hatchetAgentService;
	@Autowired
	private IHatchetIssBankService issBankService;
	@Autowired
	private IHatchetAcqService hatchetAcqService ;
	
	@RequiresPermissions("system:hatchetOrderPayment:view")
	@GetMapping("/{status}")
	public String hatchetOrderPayment(@PathVariable("status") Integer status)
	{
		if (status == 1) {
			//交易流水页面
			return prefix + "/hatchetOrderPaymentForDLS";
		}
		if (status == 2) {
			//代还交易流水页面
			return prefix + "/hatchetOrderPaymentForDLS1";
		}
		return null;
	}
	
	/**
	 * 查询直接/全部商户订单列表
	 */
	@RequiresPermissions("system:hatchetOrderPayment:list")
	@PostMapping("/list/{status}")
	@ResponseBody
	public TableDataInfo list(@PathVariable("status") Integer status,HatchetOrderPayment hatchetOrderPayment)
	{
		//通过商户编号 查询该商户整个链下面的订单
		if (StringUtils.isNotEmpty(hatchetOrderPayment.getMerchantNo())) {
			HatchetMerchant merchant = hatchetMerchantService.selectHatchetMerchantByNo(hatchetOrderPayment.getMerchantNo());
			if (StringUtils.isNotNull(merchant)) {
				/*List<HatchetMerchant> merchantList = new ArrayList<>();
				//通过商户手机号查询整个商户链
				List<HatchetMerchant> merchantTree = hatchetMerchantService.getMerchantTree(merchantList, merchant.getPHONE());
				List<String> merchantIds = new ArrayList<>();
				for (HatchetMerchant hatchetMerchant : merchantTree) {
					merchantIds.add(hatchetMerchant.getID());
				}
				merchantIds.add(merchant.getID());
				hatchetOrderPayment.setMerchantIds(merchantIds);*/
				hatchetOrderPayment.setMerchantId(merchant.getID());
			}else {
				return getDataTable(new ArrayList<>());
			}
		//代理商查询交易
		}
		if (StringUtils.isNotEmpty(hatchetOrderPayment.getAgentName())) {
			HatchetAgent paramAgent = new  HatchetAgent();
			paramAgent.setAgentName(hatchetOrderPayment.getAgentName());
			List<HatchetAgent> selectHatchetAgentList = hatchetAgentService.selectHatchetAgentList(paramAgent);
			HatchetAgent agent1 = null;
			if (StringUtils.isNotEmpty(selectHatchetAgentList)) {
				agent1 = selectHatchetAgentList.get(0);
			}
			 if (agent1 != null) {
				 HatchetAgent paramagent = hatchetAgentService.selectHatchetAgentByNo(getLoginName());
				//查直接代理商的直接商户列表
				if (status==0) {
					if (!agent1.getParentId().equals(paramagent.getID()) ) {
						//不是自己的直接下级代理商
						return getDataTable(new ArrayList<>());
					}
					hatchetOrderPayment.setAgentId(agent1.getID());
				//查所有代理商的所有商户列表
				} else {
					//判断该用户是否是代理商的下属用户
					String loginName = getLoginName();
					HatchetAgent loginAgent = hatchetAgentService.selectHatchetAgentByNo(loginName);
					HatchetAgent paramAgent1 = new HatchetAgent();
					paramAgent1.setDepartmentCode(StringUtils.likeDepartmentCode(loginAgent.getDepartmentCode()));
					List<HatchetAgent> agentTree = hatchetAgentService.selectHatchetAgentTree(paramAgent1);
					boolean isChild = false;//查询代理商是否是登陆代理商下级
					for (HatchetAgent hatchetAgent : agentTree) {
						agent1.getID().equals(hatchetAgent.getID());
						isChild = true;
						break;
					}
					if (isChild) {
						hatchetOrderPayment.setDepartmentCode(StringUtils.likeDepartmentCode(agent1.getDepartmentCode()));
					//不属于
					}else {
						return getDataTable(new ArrayList<>());
					}
				}
			} else {
				return getDataTable(new ArrayList<>());
			}
		} else {
			//查询代理商自己的信息
			String loginName = getLoginName();
			HatchetAgent paramagent = hatchetAgentService.selectHatchetAgentByNo(loginName);
			//查直接
			if (status==0) {
				hatchetOrderPayment.setAgentId(paramagent.getID());
			//查所有
			} else {
				hatchetOrderPayment.setDepartmentCode(StringUtils.likeDepartmentCode(paramagent.getDepartmentCode()));
			}
		}
		int index = 0;
		startPage();
        List<HatchetOrderPayment> list = hatchetOrderPaymentService.selectOrderPaymentForDLS(hatchetOrderPayment);
		for (HatchetOrderPayment hatchetOrderPayment2 : list) {
			//设置总金额
			if (index == 0) {
				BigDecimal sumMoney = hatchetOrderPaymentService.selectSumMoney(hatchetOrderPayment);
				hatchetOrderPayment2.setSumMoney(sumMoney);
				index++;
			}
			//银行卡加*
			if (StringUtils.isNotEmpty(hatchetOrderPayment2.getCardNo())) {
				hatchetOrderPayment2.setCardNo(StringUtils.idCardNoString(hatchetOrderPayment2.getCardNo(),4));
			}
			if (hatchetOrderPayment2.getMerchantId() != null) {
				HatchetMerchant hatchetMerchant = hatchetMerchantService.selectHatchetMerchantById(hatchetOrderPayment2.getMerchantId());
				if (hatchetMerchant !=null) {
					hatchetOrderPayment2.setMerchantNo(hatchetMerchant.getMerchantNo());
					hatchetOrderPayment2.setMerchantName(hatchetMerchant.getMerchantCnName());
				}
			}
			if (hatchetOrderPayment2.getAgentId() != null) {
				HatchetAgent agent = hatchetAgentService.selectHatchetAgentById(hatchetOrderPayment2.getAgentId());
				if (agent != null) {
					hatchetOrderPayment2.setAgentNo(agent.getAgentNo());
					hatchetOrderPayment2.setAgentName(agent.getAgentName());
				}
			}
			if (hatchetOrderPayment2.getIssBankId() != null) {
				HatchetIssBank issBank = issBankService.selectHatchetIssBankById(hatchetOrderPayment2.getIssBankId());
				if (issBank != null) {
					hatchetOrderPayment2.setIssBankId(issBank.getShortCnName());
				}
			}
			//获取通道信息
			if (hatchetOrderPayment2.getAcqId() != null) {
				HatchetAcq hatchetAcq = hatchetAcqService.selectHatchetAcqById(hatchetOrderPayment2.getAcqId());
				if (hatchetAcq != null) {
					hatchetOrderPayment2.setAcqNo(hatchetAcq.getAcqCode());
					hatchetOrderPayment2.setAcqName(hatchetAcq.getAcqName());
				}
			}
		}
        return getDataTable(list);
	}
	
	/**
	 * 查询订单列表(代还)
	 *//*
	@RequiresPermissions("system:hatchetOrderPayment:list")
	@PostMapping("/listReplace")
	@ResponseBody
	public TableDataInfo listReplace(HatchetOrderPayment hatchetOrderPayment)
	{
		//通过商户编号
		if (StringUtils.isNotEmpty(hatchetOrderPayment.getMerchantNo())) {
			HatchetMerchant merchant = hatchetMerchantService.selectHatchetMerchantByNo(hatchetOrderPayment.getMerchantNo());
			if (merchant != null) {
				hatchetOrderPayment.setMerchantId(merchant.getID());
			} else {
				 return getDataTable(new ArrayList<>());
			}
		} else if (StringUtils.isNotEmpty(hatchetOrderPayment.getAgentNo())) {
				HatchetAgent agent1 = hatchetAgentService.selectHatchetAgentByNo(hatchetOrderPayment.getAgentNo());
				if (agent1 != null) {
					hatchetOrderPayment.setAgentId(agent1.getID());
				} else {
					 return getDataTable(new ArrayList<>());
				}
			//查当前登陆的代理商的所有直接和间接商户订单
			}else {
				//查询该代理商的商户
				String loginName = getLoginName();
				//查询代理商自己的信息
				HatchetAgent paramagent = hatchetAgentService.selectHatchetAgentByNo(loginName);
				//查询自己下属的商户集合
				hatchetOrderPayment.setDepartmentCode(StringUtils.likeDepartmentCode(paramagent.getDepartmentCode()));
			}
		
		startPage();
        List<HatchetOrderPayment> list = hatchetOrderPaymentService.selectOrderPaymentReplaceForDLS(hatchetOrderPayment);
		for (HatchetOrderPayment hatchetOrderPayment2 : list) {
			//银行卡加*
			if (StringUtils.isNotEmpty(hatchetOrderPayment2.getCardNo())) {
				hatchetOrderPayment2.setCardNo(StringUtils.idCardNoString(hatchetOrderPayment2.getCardNo()));
			}
			if (hatchetOrderPayment2.getMerchantId() != null) {
				HatchetMerchant hatchetMerchant = hatchetMerchantService.selectHatchetMerchantById(hatchetOrderPayment2.getMerchantId());
				if (hatchetMerchant !=null) {
					hatchetOrderPayment2.setMerchantNo(hatchetMerchant.getMerchantNo());
					hatchetOrderPayment2.setMerchantName(hatchetMerchant.getMerchantCnName());
				}
			}
			if (hatchetOrderPayment2.getAgentId() != null) {
				HatchetAgent agent = hatchetAgentService.selectHatchetAgentById(hatchetOrderPayment2.getAgentId());
				if (agent != null) {
					hatchetOrderPayment2.setAgentNo(agent.getAgentNo());
					hatchetOrderPayment2.setAgentName(agent.getAgentName());
				}
			}
			if (hatchetOrderPayment2.getIssBankId() != null) {
				HatchetIssBank issBank = issBankService.selectHatchetIssBankById(hatchetOrderPayment2.getIssBankId());
				if (issBank != null) {
					hatchetOrderPayment2.setIssBankId(issBank.getShortCnName());
				}
			}
			//获取通道信息
			if (hatchetOrderPayment2.getAcqId() != null) {
				HatchetAcq hatchetAcq = hatchetAcqService.selectHatchetAcqById(hatchetOrderPayment2.getAcqId());
				if (hatchetAcq != null) {
					hatchetOrderPayment2.setAcqNo(hatchetAcq.getAcqCode());
					hatchetOrderPayment2.setAcqName(hatchetAcq.getAcqName());
				}
			}
		}
        return getDataTable(list);
	}
	*/
	
}
